<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link href="css/base.css" rel="stylesheet">
    <link href="css/head.css" rel="stylesheet">
    <link href="css/kind.css" rel="stylesheet">

</head>
<body>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/head.js"></script>
    <script src="js/upload.js"></script>
    <script src="js/Utils.js"></script>
    <script src="js/mock.js"></script>
    <script src="js/mock/kind.js"></script>

    <div class="head"></div>

    <div class="main">
        <div class="kindBox"></div>
        
        <div class="button" id="addKind" style="width:50%;margin:auto;margin-top:20px;">添加分类</div>

    </div>


    <script>
        $('docuemnt').ready(function(){

            initEvent()
            $.ajax({
                url : baseUrl + 'pet_types.php',
                type : 'GET',
                success : function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        renderKindBox(res.data)

                    }else{

                    }
                }
            })
        })

        function initEvent(){
            $('#addKind').click(function(){
                location.href = "add_kind.html"
            })
        }

        function renderKindBox(data){
            for(let i = 0; i < data.length; i++){
                $('.kindBox').append(`
                    <div class="kind">
                        <div class="image">
                            <img src="${baseUrl + data[i].image}">
                        </div>

                        <div class="name">
                            ${data[i].type}
                        </div>

                        <div class="operation">
                            <div class="button" onclick="changeKind(${data[i].id})">修改</div>
                            <div class="button" onclick="delKind(${data[i].id})">删除</div>
                        </div>
                    </div>
                `)
            }
        }

        function changeKind(id){
            location.href = "change_kind.html?id="+id

        }

        function delKind(id){
            
            let data = {'id':id}

            data = JSON.stringify(data)

            $.ajax({
                data,
                url:baseUrl + 'del_type.php',
                type: 'POST',
                success: function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        alert(res.msg)
                        location.reload()
                    }else
                        alert(res.msg)
                }
            })
        }
    </script>

</body>
</html>